package trlygjj.tanruiliyigenjinjin.web.listener;

import trlygjj.tanruiliyigenjinjin.util.DateUtil;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by user on 2018/7/15.
 */

/**
 *用来定时插入考勤表
 */
public class AttenceListener implements ServletContextListener {
    private static Properties properties = new Properties();
    static{
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");
        try {
            properties.load(in);
            //加载MySql的驱动类
            Class.forName(properties.getProperty("jdbc.driverClassName")) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try{
                    Connection connection = DriverManager.getConnection(properties.getProperty("jdbc.url"), properties.getProperty("jdbc.username"), properties.getProperty("jdbc.password"));
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    String date = format.format(new Date());
                    //查询当天有没有考勤
                    Statement queryAttenceByEmployeeIdAndToday = connection.createStatement();
                    String queryStr = "select id from attence where month(today) = month('" + date + "')";
                    ResultSet result = queryAttenceByEmployeeIdAndToday.executeQuery(queryStr);
                    //如果当天没有考勤就插入
                    if(result == null || !result.next()){
                        Statement statement = connection.createStatement();
                        String query = "select e.id,d.sn from employee e left join department d on e.department_id = d.id";
                        ResultSet resultSet = statement.executeQuery(query);
                        while (resultSet.next()){
                            String insert = "insert into attence(employee_id, sn, today) values(" + resultSet.getLong("id") + "," + resultSet.getString("sn") + ",'" + date +"')";
                            Statement state = connection.createStatement();
                            state.executeUpdate(insert);
                            state.close();
                        }
                    }
                }catch(Exception e){
                    //System.out.println("找不到驱动程序类 ，加载驱动失败！");
                    e.printStackTrace() ;
                }
            }
        }, DateUtil.weeHours(new Date(),0),1000 * 3600 * 24);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("项目关闭了");
    }
}
